import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import getInnerTransData from "./functions/getInnerTransData";
import checkInnerTransData from "./functions/checkInnerTransData";
import AlertModal from "../AlertModal";

const {NCModal, NCButton} = base;
const {Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter} = NCModal;

class InnerTransModal extends Component {
    constructor(props) {
        super(props);
        this.getEditInitInfo = this.getEditInitInfo.bind(this);
        this.closeModal = this.closeModal.bind(this);
        this.beSure = this.beSure.bind(this);
        this.onBeforeFormEvent = this.onBeforeFormEvent.bind(this);
        this.onAfterFormEvent = this.onAfterFormEvent.bind(this);
    }

    getEditInitInfo() {
        this.props.form.setFormStatus('innertrans', 'edit');
        getInnerTransData.call(this);
    }

    beSure() {
        const flag = this.props.form.isCheckNow('innertrans');
        if (!flag) return;
        checkInnerTransData.call(this);
    }

    closeModal() {
        this.props.onClose && this.props.onClose();
    }

    onBeforeFormEvent(props, moduleId, key, value, index) {
        if (key === 'pk_org' || key === 'pk_dept' || key === 'pk_psncl') {
            let that = this;
            let meta = that.props.meta.getMeta();
            const {form, orgVal} = that.props;
            switch (key) {
                case  'pk_org':
                    meta[moduleId].items.find(item => item.attrcode === key).queryCondition = function () {
                        return {
                            "pk_org": orgVal.refpk,//主组织
                            "TreeRefActionExt": "nccloud.web.hrtrn.dimissionrds.sqlbuilder.TransAdminOrgRefSqlBuilder"
                        }
                    };
                    break;
                case 'pk_dept':
                    const newOrg = form.getFormItemsValue('innertrans', 'pk_org');
                    if (newOrg.value) {
                        meta[moduleId].items.find(item => item.attrcode === key).queryCondition = function () {
                            return {
                                "pk_org": newOrg.value,//组织参照选择的值  不是主组织
                                "TreeRefActionExt": "nccloud.web.hrtrn.dimissionrds.sqlbuilder.TransDeptRefSqlBuilder"
                            }
                        };
                    }
                    break;
                case  'pk_psncl':
                    meta[moduleId].items.find(item => item.attrcode === key).queryCondition = function () {
                        return {"TreeRefActionExt": "nccloud.web.hrhi.pub.sqlbuilder.PsnclRefSqlBuilder"}
                    };
                    break;
                default:
                    break;
            }

            that.props.meta.setMeta(meta);
        }
        return true;
    }

    onAfterFormEvent(props, moduleId, key, value, oldValue) {
        if (key === 'pk_org') {
            this.props.form.setFormItemsValue('innertrans', {
                'pk_dept': {
                    value: null,
                    display: null
                }
            })
        }
    }

    render() {
        const {visible, form} = this.props;
        return (
            <div>
                <NCModal
                    show={visible}
                    size="lg"
                    onHide={this.closeModal}
                    onEntered={this.getEditInitInfo}
                    backdrop={'static'}
                >
                    <NCModalHeader
                        closeButton={true}
                    >
                        {this.props.json['hi6007-000163']/* 国际化处理： 组织内转移*/}
                    </NCModalHeader>
                    <NCModalBody>
                        {
                            form.createForm('innertrans', {
                                onBeforeEvent: this.onBeforeFormEvent,
                                onAfterEvent: this.onAfterFormEvent
                            })
                        }
                    </NCModalBody>
                    <NCModalFooter>
                        <NCButton
                            colors="primary"
                            onClick={this.beSure}
                        >
                            {this.props.json['hi6007-000174']}{/* 国际化处理： 确认*/}
                        </NCButton>
                        <NCButton
                            shape="border"
                            onClick={this.closeModal}
                        >
                            {this.props.json['hi6007-000005']/* 国际化处理： 取消*/}
                        </NCButton>
                    </NCModalFooter>
                </NCModal>

                <AlertModal ref={node => this.alertModalNode = node} json={this.props.json}/>
            </div>
        );
    }
}

export default InnerTransModal;
